/*******************************************************************************
 * 创建: 2024年08月28日
 * 作者: 水煮肉片饭(27185709@qq.com)
 * 描述: 调色板，实现cc.Sprite或cc.Label颜色渐变
 *******************************************************************************/
import * as cc from 'cc';
import { DEV, JSB } from 'cc/env';
const { ccclass, property, executeInEditMode, requireComponent, menu } = cc._decorator;
@ccclass
@executeInEditMode
@requireComponent(cc.UIRenderer)
@menu('Public/Palette')
export class Palette extends cc.Component {
    @property
    private _colorLB: cc.Color = new cc.Color(255, 255, 255, 255);
    @property({ displayName: DEV && '↙ 左下' })
     get colorLB() { return this._colorLB };
     set colorLB(value: cc.Color) {
        this._colorLB = value;
        this.updateColor();
    }
    @property
    private _colorRB: cc.Color = new cc.Color(255, 255, 255, 255);
    @property({ displayName: DEV && '↘ 右下' })
     get colorRB() { return this._colorRB };
     set colorRB(value: cc.Color) {
        this._colorRB = value;
        this.updateColor();
    }
    @property
    private _colorLT: cc.Color = new cc.Color(255, 255, 255, 255);
    @property({ displayName: DEV && '↖ 左上' })
    get colorLT() { return this._colorLT };
    set colorLT(value: cc.Color) {
        this._colorLT = value;
        this.updateColor();
    }
    @property
    private _colorRT: cc.Color = new cc.Color(255, 255, 255, 255);
    @property({ displayName: DEV && '↗ 右上' })
     get colorRT() { return this._colorRT };
     set colorRT(value: cc.Color) {
        this._colorRT = value;
        this.updateColor();
    }
    private ur: cc.UIRenderer = null;
    protected onLoad() {
        this.ur = this.node.getComponent(cc.UIRenderer);
        if (this.ur['_renderData'].chunk.vb.length !== 36) {
            cc.warn('Palette只对cc.Sprite和cc.Label有效！');
            this.destroy();
            return;
        }
        this.ur['_useVertexOpacity'] = true;//启用顶点透明度（否则透明度只受color.a影响）
        JSB && cc.director.once(cc.Director.EVENT_AFTER_DRAW, this.updateColor, this);
    }
    private updateColor() {
        let vb = this.ur['_renderData'].chunk.vb;
        let lb = this._colorLB, rb = this._colorRB, lt = this._colorLT, rt = this._colorRT;
        vb[5] = lb.r / 255; vb[6] = lb.g / 255; vb[7] = lb.b / 255; vb[8] = lb.a / 255;
        vb[14] = rb.r / 255; vb[15] = rb.g / 255; vb[16] = rb.b / 255; vb[17] = rb.a / 255;
        vb[23] = lt.r / 255; vb[24] = lt.g / 255; vb[25] = lt.b / 255; vb[26] = lt.a / 255;
        vb[32] = rt.r / 255; vb[33] = rt.g / 255; vb[34] = rt.b / 255; vb[35] = rt.a / 255;
    }
    protected onEnable() {
        this.updateColor();
    }
    protected onDisable() {
        let vb = this.ur['_renderData'].chunk.vb;
        let color = this.ur.color;
        vb[5] = vb[14] = vb[23] = vb[32] = color.r / 255;
        vb[6] = vb[15] = vb[24] = vb[33] = color.g / 255;
        vb[7] = vb[16] = vb[25] = vb[34] = color.b / 255;
        vb[8] = vb[17] = vb[26] = vb[35] = color.a / 255;
    }
}